package es

import (
	"gin-easy/pkg/viper_config"
	"github.com/olivere/elastic/v7"
	"log"
	"os"
)

var Elastic *elastic.Client

func Setup() {
	elasticConfig := viper_config.Conf.ES
	if elasticConfig.Enable {
		client, err := elastic.NewClient(
			elastic.SetURL(elasticConfig.Url),
			elastic.SetSniff(elasticConfig.Sniff),
			elastic.SetHealthcheckInterval(elasticConfig.HealthCheckInterval),
			elastic.SetErrorLog(log.New(os.Stderr, "ELASTIC ", log.LstdFlags)), //ERROR日志输出配置
			elastic.SetInfoLog(log.New(os.Stdout, "", log.LstdFlags)),          //INFO级别日志输出配置
		)
		if err != nil {
			panic("创建ES客户端错误:" + err.Error())
		}
		Elastic = client
	}
}
